<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class admins extends MX_Controller
{
	function admins()
	{
		parent::__construct();
		$this->load->model('login','admin');
		$this->load->model('mod_lib');
	}
	
	function index()
	{
		$data['title'] 					= 'Login Administrator';
		if(is_dir('captcha/'))
		{
			@delete_files('captcha/', true);
			@write_file('captcha/index.html', '<p>Directory access is forbidden.</p>');
		}
		$this->load->helper('file');
		if(is_dir('application/cache/'))
		{
			@delete_files('application/cache/', true);
			@write_file('application/cache/index.html', '<p>Directory access is forbidden.</p>');
		}
		$this->load->view('back_end/login', $data);
	}
	
	function action_login()
	{
		$username = trim($this->input->post('username'));
		$password = md5(md5($this->input->post('password')));
		if (!empty($username) && !empty($password))
		{
			$this->admin->login_admin($username, $password);
		}
		else
		{
			$_SESSION["error_loginadmin"] = "Bạn chưa nhập Username | Password";
			redirect(base_url().'quikr/admins');
		}
	}
	
	function index_admin()
	{
		if (!isset($_SESSION['admin_login']) && empty($_SESSION['admin_login']))
		{
			redirect(base_url().'quikr/admins');
		}
		else
		{
			$data['title']	= 'Welcome to Quikr';
			$current 		= time();
			$current_date 	= strtotime(date('m/d/Y', $current));
			$time_out		= 1800;
			$time_new		= $current - $time_out;
			
// 			$data['total_member']			= $this->mod_lib->count('member', array());
// 			$data['total_member_active']	= $this->mod_lib->count('member', array('status'=>1));
// 			$data['total_member_today']		= $this->mod_lib->count('member', array('createdate >'=>$current_date, 'createdate <'=>$current));
			
// 			$data['total_product']			= $this->mod_lib->count('article', array());
// 			$data['total_product_active']	= $this->mod_lib->count('article', array('status'=>1));
// 			$data['total_product_today']	= $this->mod_lib->count('article', array('createdate >'=>$current_date, 'createdate <'=>$current));
			
			$data['total_ads']				= $this->mod_lib->count('ads', array());
			$data['total_ads_active']		= $this->mod_lib->count('ads', array('status'=>1));
			$data['total_ads_expires']		= $this->mod_lib->count('ads', array('enddate <'=>$current));
			$data['total_ads_today']		= $this->mod_lib->count('ads', array('createdate >'=>$current_date, 'createdate <'=>$current));
			
// 			$data['total_contact']			= $this->mod_lib->count('contact', array());
// 			$data['total_contact_active']	= $this->mod_lib->count('contact', array('status'=>1));
// 			$data['total_contact_unactive']	= $this->mod_lib->count('contact', array('status'=>0));
// 			$data['total_contact_today']	= $this->mod_lib->count('contact', array('createdate >'=>$current_date, 'createdate <'=>$current));
			
			$data['total_counter']				= $this->mod_lib->count('counter', array());
			$data['total_counter_today']		= $this->mod_lib->count('counter', array('date_time >'=>$current_date, 'date_time <'=>$current));
			$data['total_counter_yesterday']	= $this->mod_lib->count('counter', array('date_time <'=>$current_date, 'date_time >'=>$current_date - 24*60*60));
			
// 			$sql			= "DELETE FROM hba_online WHERE date_time < $time_new";
// 			$this->db->query($sql);
// 			$data['total_online']			= $this->mod_lib->count('online', array());
			
			$data['content_admin']	= 'back_end/content_admin';
			$this->load->view('back_end/template_admin', $data);
		}
	}
	
	function logout()
	{
		$_SESSION['show']				= NULL;
		$_SESSION['admin_id']			= NULl;
		$_SESSION['admin_login']		= NULL;
		$_SESSION['username']			= NULL;
		$_SESSION['error_loginadmin']	= NULL;
		redirect(base_url().'quikr/admins');
	}
}